package online.chazi.bootplus.multiple.datasource.router;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 说明：轮询
 * Created by 叉子同学 on 2020-05-13 19:58
 */
@Slf4j
public class MultipleDataSourcePollingRouter implements MultipleDataSourceBalanceRouter {

    private AtomicInteger COUNTER = new AtomicInteger(0);

    @Override
    public void initWeight(String name, int weight) {
    }

    @Override
    public String determineLookupKey(String ds, List<String> targetDataSources) {
        int index = COUNTER.incrementAndGet() % targetDataSources.size();
        String name = targetDataSources.get(index);
        log.info("使用polling轮询。group={}, name={}, counter={}, index={}", ds, name, COUNTER.get(), index);
        return name;
    }

}
